#ifndef VALUE_H_
#define VALUE_H_

#include <stdbool.h>
#include <stdint.h>
#include <time.h>

typedef enum _value_class_e {
    vc_nvl = '!', vc_bit = 'b', vc_short = 'h', vc_int = 'i', vc_long = 'l', vc_float = 'f', vc_double = 'd'
} value_class_e;

typedef enum _quality_e {
    good,
    failure,
    oldData,
    outOfRange,
    overFlow,
    suspect,
    estimatorReplaced,
    operatorBlocked,
    oscillatory,
    test
} quality_e;

typedef struct _value_id_t {
    uint32_t chn_id;
    uint32_t dev_id;
    uint32_t seg_id;
    uint32_t tag_id;
} value_id_t;

typedef struct _value_t {
    value_class_e clazz;
    uint8_t size;
    quality_e quality;//数据质量
    _Bool _changed; //数据发生变化

    //原值
    union {
        _Bool bval;
        int64_t ival;
        float fval;
        double dval;
        long long int _val;
    };

    //最终值
    float value;
    //时间戳
    time_t timestamp;
} value_t;

value_t *value_new();

void value_free(value_t *value);

void value_set_null(value_t *value);

void value_set_bit(value_t *value, _Bool val);

void value_set_short(value_t *value, int16_t val);

void value_set_int(value_t *value, int32_t val);

void value_set_long(value_t *value, int64_t val);

void value_set_float(value_t *value, float val);

void value_set_double(value_t *value, double val);

void value_printf(char *buf, const value_t *value);

void value_scanf(char *buf, value_t *value);

void value_copy(value_t *dst, const value_t *src);

char *bin2str(const uint8_t *bin, size_t nbin, char *str, size_t *nstr);

uint8_t *str2bin(const char *str, uint8_t *bin, size_t *nbin);

uint8_t *swap_byte(uint8_t *ptr, size_t size);

uint16_t *swap_word(uint16_t *ptr, size_t size);

uint32_t *swap_dword(uint32_t *ptr, size_t size);

#endif /* VALUE_H_ */
